//公用连表
const {
  modelArticle,
  modelUser,
  modelLike,
  modelCollection,
  modelComment,
  modelSignUp,
  modelCompanios,
} = require("@/models/collection");

const looKup = () => {
  const model_user = {
    //用户表
    $lookup: {
      from: modelUser.collection.name,
      localField: "author_uid",
      foreignField: "uid",
      as: "author_data",
    },
  };
  const model_like = {
    //点赞表
    $lookup: {
      from: modelLike.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "likes",
    },
  };
  const model_comment = {
    //评论表
    $lookup: {
      from: modelComment.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "comments",
    },
  };
  const model_collection = {
    //收藏表
    $lookup: {
      from: modelCollection.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "collections",
    },
  };
  // 游记详情页的公用连表
  const projectcard = {
    $project: {
      _id: 1,
      title: 1,
      cover_image: 1,
      address: 1,
      fileType: 1,
      "author_data.avatarUrl": 1,
      "author_data.nickname": 1,
      likes: { $size: "$likes" },
    },
  };
  return {
    model_user,
    model_like,
    model_comment,
    model_collection,
    projectcard,
  };
};

//四个推荐游记的公用连表
const looKupRecommend = () => {
  const model_Article = {
    //游记表
    $lookup: {
      from: modelArticle.collection.name,
      localField: "travel_id",
      foreignField: "_id",
      as: "articleData",
    },
  };
  const model_reco_user = {
    //用户表
    $lookup: {
      from: modelUser.collection.name,
      localField: "articleData.author_uid",
      foreignField: "uid",
      as: "userData",
    },
  };
  return {
    model_Article,
    model_reco_user,
  };
};

//获取用户关注的作者信和游记
const looKupConcer = (_id) => {
  const model_like = {
    //点赞表
    $lookup: {
      from: modelLike.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "likes",
    },
  };
  const model_comment = {
    //评论表
    $lookup: {
      from: modelComment.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "comments",
    },
  };
  const model_collection = {
    //收藏表
    $lookup: {
      from: modelCollection.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "collections",
    },
  };
  return {
    model_like,
    model_comment,
    model_collection,
  };
};
// 旅游结伴：首页筛选活动
const looKupCompanion = () => {
  const model_user = {
    //用户表
    $lookup: {
      from: modelUser.collection.name,
      localField: "uid",
      foreignField: "uid",
      as: "author_data",
    },
  };
  const model_signup = {
    //报名表
    $lookup: {
      from: modelSignUp.collection.name,
      localField: "_id",
      foreignField: "signup_id",
      as: "signups",
    },
  };
  const model_companion = {
    //活动表
    $lookup: {
      from: modelCompanios.collection.name,
      localField: "signup_id",
      foreignField: "_id",
      as: "my_companion",
    },
  };

  return {
    model_signup,
    model_user,
    model_companion,
  };
};

module.exports = {
  looKup,
  looKupRecommend,
  looKupConcer,
  looKupCompanion,
};
